import Vue from 'vue'
import Router from 'vue-router'

const login = () => import(/* webpackChunkName: "login-home-welcome" */ './components/login.vue')
const home = () => import(/* webpackChunkName: "login-home-welcome" */ './components/home.vue')
const welcome = () => import(/* webpackChunkName: "login-home-welcome" */ './components/welcome.vue')
const users = () => import(/* webpackChunName: "users-rights-roles" */ './components/users.vue')
const rights = () => import(/* webpackChunName: "users-rights-roles" */ './components/power/rights.vue')
const roles = () => import(/* webpackChunName: "users-rights-roles" */ './components/power/roles.vue')
const cate = () => import(/* webpackChunName: "cate-params-list" */ './components/goods/cate.vue')
const params = () => import(/* webpackChunName: "cate-params-list" */ './components/goods/params.vue')
const list = () => import(/* webpackChunName: "cate-params-list" */ './components/goods/list.vue')
const add = () => import(/* webpackChunName: "add-orders-reports" */ './components/goods/add.vue')
const orders = () => import(/* webpackChunName: "add-orders-reports" */ './components/orders/orders.vue')
const reports = () => import(/* webpackChunName: "add-orders-reports" */ './components/reports/reports.vue')

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: welcome },
        { path: '/users', component: users },
        { path: '/rights', component: rights },
        { path: '/roles', component: roles },
        { path: '/categories', component: cate },
        { path: '/params', component: params },
        { path: '/goods', component: list },
        { path: '/goods/add', component: add },
        { path: '/orders', component: orders },
        { path: '/reports', component: reports }
      ]
    }
  ]
})
// 挂载路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
